package com.lantian.yjmall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.lantian.yjmall.product.entity.ProductAttrValueEntity;
import com.lantian.yjmall.product.service.ProductAttrValueService;
import com.lantian.yjmall.product.vo.AttrGroupRelationVo;
import com.lantian.yjmall.product.vo.AttrRespVo;
import com.lantian.yjmall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lantian.yjmall.product.entity.AttrEntity;
import com.lantian.yjmall.product.service.AttrService;
import com.lantian.common.utils.PageUtils;
import com.lantian.common.utils.R;



/**
 * 商品属性
 *
 * @author lyj
 * @email 945874570@qq.com
 * @date 2020-05-31 15:55:51
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     * 规格参数或销售属性页面列表
     */
    @GetMapping("{attrType}/list/{catelogId}")
    public R baseAttrList(
            @RequestParam Map<String, Object> params,
            @PathVariable("catelogId") Long catelogId,
            @PathVariable("attrType") String Type ){
        PageUtils page = attrService.queryBaseAttrPage(params, catelogId, Type);
        if (page!= null) {
            return R.ok().put("page", page);
        }
        return R.error();
    }

    /**
     *  回显已发布商品的规格属性
     * @param spuId
     * @return
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R baseAttrlistforspu(@PathVariable("spuId") Long spuId){

        List<ProductAttrValueEntity> entities = productAttrValueService.baseAttrlistforspu(spuId);

        return R.ok().put("data",entities);
    }

    /**
     *  修改已发布商品的规格属性
     * @param spuId
     * @param entities
     * @return
     */
    @PostMapping("/update/{spuId}")
    public R updateSpuAttr(@PathVariable("spuId") Long spuId,
                           @RequestBody List<ProductAttrValueEntity> entities){

        productAttrValueService.updateSpuAttr(spuId,entities);

        return R.ok();
    }


    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
		AttrRespVo respVo = attrService.getAttrInfo(attrId);
		if (respVo != null) {
            return R.ok().put("attr", respVo);
        }
		return R.error();


    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attrVo){
        boolean save = attrService.saveAttr(attrVo);
        if (save) {
            return R.ok();
        } else {
            return R.error();
        }


    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody AttrVo attr){
		boolean flag =  attrService.updateAttr(attr);
		if (flag) {
            return R.ok();
        } else {
		    return R.error();
        }

    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
